import "./DrawerAddUpdate.less";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, useForm, FormSchema } from "@/components/Form";
import { create_emergencies, update_emergencies } from "@/api/EmergencyDictionary";
import { useTranslation } from "react-i18next";
const UserRoleDrawer = (props: { register: Fn; reload: Fn; getTitle: string }) => {
	const { t } = useTranslation();
	const userRoleFormSchema: FormSchema[] = [
		{
			label: t("felix_ui.time"),
			field: "time",
			component: "InputNumber",
			colProps: { span: 24 }
		},
		// {
		// 	label: t("felix_ui.type"),
		// 	field: "type",
		// 	component: "Select",
		// 	colProps: { span: 24 },
		// 	componentProps: {
		// 		options: [
		// 			{ label: "卫生机构", value: "1" },
		// 			{ label: "未提及", value: "2" },
		// 			{ label: "奥尔瑟", value: "3" }
		// 		]
		// 	}
		// },
		{
			label: t("felix_ui.beneficiary"),
			field: "beneficiary",
			component: "Select",
			colProps: { span: 24 },
			componentProps: {
				options: [
					{ label: "健康保险", value: "1" },
					{ label: "服务费", value: "2" },
					{ label: "健康保险-HD52", value: "3" },
					{ label: "费用-TT02", value: "4" }
				]
			}
		},
		{
			label: t("felix_ui.reg_content"),
			field: "reg_content",
			component: "Input",
			colProps: { span: 24 }
		},
		{
			label: t("felix_ui.amount"),
			field: "amount",
			component: "Input",
			colProps: { span: 24 }
		}
	];
	const [registerForm, { validateFields, resetFields, setFieldsValue }] = useForm({
		labelWidth: 120,
		schemas: userRoleFormSchema,
		showActionButtonGroup: false
	});

	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		console.log("打开Drawer", data);
		resetFields();
		setFieldsValue(data.record);
	});

	async function handleSubmit() {
		let values = await validateFields();
		values = { ...values, emgDtime: Date.parse(values.emgDtime) }; // 时分秒转成时间戳
		console.log("提交", values);
		if (props.getTitle == t("felix_ui.edit_emergency_info")) {
			await update_emergencies(values);
		} else {
			await create_emergencies(values);
		}
		props.reload();
		closeDrawer();
	}

	return (
		<BasicDrawer {...props} title={props.getTitle} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={registerForm} />
		</BasicDrawer>
	);
};

export default UserRoleDrawer;
